<?php
class CmsPostTagsControl extends CmsBaseControl
{
    protected $id;
    protected $model;
    protected $templateFile = 'CmsPostTags';

    public function CmsPostTagsControl($id)
    {
        parent::__construct();
        $this->id = $id;
        $this->model = $this->GetModel();
        BaseConfig::GetInstance()->AddCss('controls/jquery.autocomplete');
        BaseConfig::GetInstance()->AddSharedJs('jquery/jquery.autocomplete');
    }

    protected function GetModel()
    {
        return CmsPostTagModel::GetInstance();
    }

    protected function GetRefreshUrl()
    {
        return parent::GetRefreshUrl().FS.$this->id;
    }

    public function Render()
    {
        $tags = $this->model->GetTags($this->id);
        $this->Repeat('tag', $tags);
        $this->Replace('gettags_url', $this->GetRefreshUrl().'&action=GetTags');
        return parent::Render();
    }

    public function Save($tags)
    {
        try
        {
            $this->model->Add($this->id, $tags);
        }
        catch (Exception $ex)
        {
            MessageBase::Warning($ex->getMessage());
        }
        return $this->Render();
    }

    public function GetTags()
    {
        $query = RequestHandler::Get('q');
        $tags = CmsTagModel::GetInstance()->Search($query);
        echo implode("\n", $tags);
        exit_app();
    }

    public function RemoveTag($tag)
    {
        $this->model->Remove($this->id, $tag);
        return $this->Render();
    }
}
?>
